/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.views;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;
import org.toedo.controllers.DataController;
import org.toedo.controllers.events.EventConstants;
import org.toedo.controllers.events.EventController;
import org.toedo.gui.translations.Messages;
import org.toedo.logging.Log;
import org.toedo.model.enums.WikiMarkupType;
import org.toedo.utils.ApplicationUtils;

public class WikiFormatingView extends ViewPart implements PropertyChangeListener {

	public static final String ID = "toedo-core.WikiFormatingView"; //$NON-NLS-1$
	
	private static final String WIKIFORMATING_PATH = "org/toedo/resources/wikiformating/"; //$NON-NLS-1$
	
	private Composite mParent;
	private Browser mBrowser;
	
	private WikiMarkupType mMarkupType;
	
	public WikiFormatingView() {
		
	}

	@Override
	public void createPartControl(Composite parent) {
		mParent = parent;
		
		mBrowser = new Browser(mParent, SWT.NONE);
		
		mBrowser.addLocationListener(new LocationListener() {
			@Override
			public void changed(LocationEvent event) {
				// TODO Auto-generated method stub				
			}
			@Override
			public void changing(LocationEvent event) {
				if (!event.location.equals("about:blank")) { //$NON-NLS-1$
					event.doit = false;
					ApplicationUtils.openLinkInBrowser(event.location, true);
				}
			}
		});
		
		fillContent();
		
		EventController.getInstance().addGeneralEventsListener(this);
	}

	private void setViewTitle() {
		switch(mMarkupType) {
		case EnTextile : setPartName(Messages.WikiFormatingView_TextileTitle); break;
		case EnMediawiki : setPartName(Messages.WikiFormatingView_MediawikiTitle); break;
		case EnTrac : setPartName(Messages.WikiFormatingView_TracTitle); break;
		case EnConfluence : setPartName(Messages.WikiFormatingView_ConfluenceTitle); break;
		default : setPartName(Messages.WikiFormatingView_TextileTitle);
		}
	}
	
	private InputStream getHelpStream() {
		String path = WIKIFORMATING_PATH;
		
		switch(mMarkupType) {
		case EnTextile : path += "textile.html"; break; //$NON-NLS-1$
		case EnMediawiki : path += "mediawiki.html"; break; //$NON-NLS-1$
		case EnTrac : path += "trac.html"; break; //$NON-NLS-1$
		case EnConfluence : path += "confluence.html"; break; //$NON-NLS-1$
		default : path += "textile.html";  //$NON-NLS-1$
		}
		
		return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
	}
	
	private void fillContent() {
		WikiMarkupType currentType = DataController.getInstance().getProject().getMarkupType();
		
		if ((mMarkupType == null) ||
				(!mMarkupType.equals(currentType))) {
			
			mMarkupType = currentType;
			
			setViewTitle();

			InputStream stream = getHelpStream();

			if (stream != null) {
				StringBuilder sb = new StringBuilder();

				try {
					InputStreamReader streamReader = new InputStreamReader(stream);
					BufferedReader buffer = new BufferedReader(streamReader);						

					String line = null;
					while ( null != (line = buffer.readLine())){
						sb.append(line + "\n"); //$NON-NLS-1$
					}
				} catch (IOException e) {
					Log.getInstance(WikiFormatingView.class).warn("Unable to read wikiformating."); //$NON-NLS-1$
				}

				mBrowser.setText(sb.toString());
			}
		}
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		class Refresher implements Runnable {
			public void run() {
				fillContent();
			}
		}
		
		if ((arg0.getPropertyName().equals(EventConstants.EVT_OPEN)) ||
				(arg0.getPropertyName().equals(EventConstants.EVT_PROJECT_PROPERTIES_CHANGE))) {
			Display.getDefault().asyncExec(new Refresher());
		}		
	}
	
	public void dispose() {
		EventController.getInstance().removeGeneralEventsListener(this);
	}

}
